/**
 * 
 */
package dt.inlee.system.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionContext;

import dt.inlee.common.constant.CommonConstant;
import dt.inlee.common.constant.CommonEnumType;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.domain.LogInfo;
import dt.inlee.domain.SysOrganization;
import dt.inlee.system.dao.LogInfoDao;
import dt.inlee.system.vo.LogInfoBean;
import dt.inlee.system.vo.SysUserLoginBean;

/**
 * @功能说明：日志业务层
 * @作者： xgyin
 * @创建日期： 2011-3-22
 */
@Service("logInfoService")
public class LogInfoService {
	@Autowired
	private LogInfoDao logInfoDao;

	/**
		 * 新增日志
		 * 
		 * @param entity
		 * @throws FrameworkException
		 */
	@Transactional
	public void addLogInfo(LogInfo entity) throws FrameworkException {
		entity = this.setLoginfo(entity);
		if (!BaseUtil.isEmpty(entity))
			this.logInfoDao.save(entity);
	}

	/**
	 * 修改日志
	 * 
	 * @param entity
	 * @throws FrameworkException
	 */
	@Transactional
	public void modifyLogInfo(LogInfo entity) throws FrameworkException {
		entity = this.setLoginfo(entity);
		if (!BaseUtil.isEmpty(entity))
			this.logInfoDao.update(entity);
	}

	/**
	 * 删除日志(逻辑删除，将状态改为不可见)
	 * 
	 * @param id
	 * @throws FrameworkException
	 */
	@Transactional
	public void deleteLogInfoById(String id) throws FrameworkException {
		LogInfo entity = (LogInfo) this.logInfoDao.get(LogInfo.class, id);
		if (!BaseUtil.isEmpty(entity)) {
			entity = this.setLoginfo(entity);
			if (!BaseUtil.isEmpty(entity)) {
				// 状态
				entity.setState(CommonEnumType.LogState.删除.getKey());
				this.logInfoDao.update(entity);
			}
		}
	}

	/**
	 * 批量删除日志(逻辑删除，将状态改为不可见)
	 * 
	 * @param ids
	 * @throws FrameworkException
	 */
	@Transactional
	public void deleteLogInfoByIds(String[] ids) throws FrameworkException {
		for (String id : ids) {
			this.deleteLogInfoById(id);
		}
	}

	/**
	 * 给日志对象某些属性赋固定值
	 * 
	 * @param entity
	 * @return
	 * @throws FrameworkException
	 */
	private LogInfo setLoginfo(LogInfo entity) throws FrameworkException {
		SysUserLoginBean loginUser = (SysUserLoginBean) ActionContext.getContext().getSession().get(
				CommonConstant.SESSION_USER);
		if (BaseUtil.isEmpty(loginUser))
			return null;
		// 操作用户
		entity.setOperatorUser(loginUser.getRealName());
		// 操作用户的ip
		entity.setIp(loginUser.getIp());
		// 操作用户的所属机构
		entity.setOperatorOrg(loginUser.getOrganizationName());
		// 操作时间
//		entity.setDtime(DateUtil.getDBTime());
		entity.setDtime(new Date());
		// 状态
		entity.setState(CommonEnumType.LogState.正常.getKey());
		//所属公司
		//SysOrganization belongsOrg = this.areaDictionaryService.findCommpanByLoginUserId(loginUser.getId());
		//entity.setBelongsCompany("");
		return entity;
	}

	/**
	 * 
	 * @param logInfoBean
	 * @return
	 * @throws FrameworkException
	 */
	public int getLogInfoTotalRows(LogInfoBean logInfoBean) throws FrameworkException {

		return this.logInfoDao.getLogInfoTotalRows(logInfoBean);
	}

	/**
	 * 
	 * @param order 
	 * @param orderBy 
	 * @param logInfoBean
	 * @param curPage
	 * @param pageSize
	 * @return
	 * @throws FrameworkException
	 */
	public List<LogInfo> findAllLogInfos(String order,String dtime, LogInfoBean logInfoBean, int curPage,
			int pageSize) throws FrameworkException {

		return this.logInfoDao.findAllLogInfos(order, dtime, logInfoBean, curPage, pageSize);
	}

	/**
	 * 
	 * @param reportType
	 * @param orgName
	 * @param beginTime
	 * @param endTime
	 * @return
	 * @throws FrameworkException
	 */
	public List<LogInfoBean> logInfoReportCount(String reportType, String orgName, Date beginTime, Date endTime)
			throws FrameworkException {
		return this.logInfoDao.logInfoReportCount(reportType, orgName, beginTime, endTime);

	}

	/**
	 * 
	 * @param reportType
	 * @param orgName
	 * @param beginTime
	 * @param endTime
	 * @return
	 * @throws FrameworkException
	 */
	public List<LogInfoBean> logInfoSumReportCount(String reportType, String orgName, Date beginTime, Date endTime)
			throws FrameworkException {
		return this.logInfoDao.logInfoSumReportCount(reportType, orgName, beginTime, endTime);
	}

	/**
	 * 所有的日志组织机构
	 * @return
	 * @throws FrameworkException
	 */
	public List<SysOrganization> findAllLogOrgs() throws FrameworkException {
		return this.logInfoDao.findAllLogOrgs();
	}
}
